package com.ruoyi.exam.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.exam.vo.ExamQuestionFavoriteVo;
import com.ruoyi.exam.vo.MyFavoriteQuestionVo;

import com.ruoyi.exam.service.IExamQuestionFavoriteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 题目收藏Controller
 *
 * @author Tellsea
 * @date 2025-05-30
 */
@Api(tags = "题目收藏Controller")
@RestController
@RequestMapping("/exam/questionFavorite")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class ExamQuestionFavoriteController extends BaseController {

    private final IExamQuestionFavoriteService examQuestionFavoriteService;

    @ApiOperation("查询题目收藏列表")
    @PreAuthorize("@ss.hasPermi('exam:questionFavorite:list')")
    @GetMapping("/list")
    public TableDataInfo<ExamQuestionFavoriteVo> list(ExamQuestionFavoriteVo entity) {
        return examQuestionFavoriteService.queryList(entity);
    }

    @ApiOperation("查询我的收藏题目列表")
    @GetMapping("/myFavorites")
    public TableDataInfo<MyFavoriteQuestionVo> myFavorites(MyFavoriteQuestionVo entity) {
        return examQuestionFavoriteService.queryMyFavoriteQuestions(getUserId(), entity);
    }

    @ApiOperation("查询题目收藏所有列表")
    @GetMapping("/listAll")
    public AjaxResult listAll(ExamQuestionFavoriteVo entity) {
        return AjaxResult.success("查询成功", examQuestionFavoriteService.queryAll(entity));
    }

    @ApiOperation("导出题目收藏列表")
    @PreAuthorize("@ss.hasPermi('exam:questionFavorite:export')")
    @Log(title = "题目收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamQuestionFavoriteVo entity) {
        List<ExamQuestionFavoriteVo> list = examQuestionFavoriteService.queryAll(entity);
        ExcelUtil<ExamQuestionFavoriteVo> util = new ExcelUtil<>(ExamQuestionFavoriteVo.class);
        util.exportExcel(response, list, "题目收藏数据");
    }

    @ApiOperation("获取题目收藏详细信息")
    @PreAuthorize("@ss.hasPermi('exam:questionFavorite:query')")
    @GetMapping(value = "/getInfo/{favoriteId}")
    public AjaxResult getInfo(@PathVariable("favoriteId") Long favoriteId) {
        return AjaxResult.success("查询成功", examQuestionFavoriteService.queryById(favoriteId));
    }

    @ApiOperation("新增题目收藏")
    @PreAuthorize("@ss.hasPermi('exam:questionFavorite:add')")
    @Log(title = "题目收藏", businessType = BusinessType.INSERT)
    @PostMapping("add")
    public AjaxResult add(@RequestBody ExamQuestionFavoriteVo entity) {
        //首先判断是否已经收藏
        ExamQuestionFavoriteVo query = new ExamQuestionFavoriteVo();
        query.setUserId(entity.getUserId());
        query.setQuestionId(entity.getQuestionId());
        List<ExamQuestionFavoriteVo> list = examQuestionFavoriteService.queryAll(query);
        if (list.size() > 0) {  
            return AjaxResult.error("已经收藏");
        }
        entity.setUserId(getUserId());
        entity.setCreateBy(getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        entity.setUpdateBy(getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return toAjax(examQuestionFavoriteService.save(entity));
    }

    @ApiOperation("取消收藏")
    @Log(title = "题目收藏", businessType = BusinessType.DELETE)
    @GetMapping("/cancel")
    public AjaxResult cancel(ExamQuestionFavoriteVo entity) {
        //通过userId和questionId查询id
        ExamQuestionFavoriteVo query = new ExamQuestionFavoriteVo();
        query.setUserId(entity.getUserId());
        query.setQuestionId(entity.getQuestionId());
        List<ExamQuestionFavoriteVo> list = examQuestionFavoriteService.queryAll(query);
        if (list.size() == 0) {
            return AjaxResult.error("未收藏");
        }
        return toAjax(examQuestionFavoriteService.removeById(list.get(0).getFavoriteId()) ? 1 : 0);
    }

    @ApiOperation("修改题目收藏")
    @PreAuthorize("@ss.hasPermi('exam:questionFavorite:edit')")
    @Log(title = "题目收藏", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody ExamQuestionFavoriteVo entity) {
        return toAjax(examQuestionFavoriteService.updateById(entity));
    }

    @ApiOperation("删除题目收藏")
    @PreAuthorize("@ss.hasPermi('exam:questionFavorite:remove')")
    @Log(title = "题目收藏", businessType = BusinessType.DELETE)
	@GetMapping("/remove/{favoriteIds}")
    public AjaxResult remove(@PathVariable Long[] favoriteIds) {
        return toAjax(examQuestionFavoriteService.removeByIds(Arrays.asList(favoriteIds)) ? 1 : 0);
    }

    @ApiOperation("获取用户收藏的指定题库的题目信息")
    @GetMapping("/bankFavoriteQuestionInfo/{bankId}")
    public AjaxResult getBankFavoriteQuestionInfo(@PathVariable("bankId") Long bankId) {
        Long userId = getUserId();
        return AjaxResult.success("查询成功", examQuestionFavoriteService.getBankFavoriteQuestionInfo(userId, bankId));
    }

}
